package com.dididoctor.patient.Activity.Service;

import android.app.Notification;
import android.app.NotificationManager;
import android.view.View;
import android.widget.AbsListView;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.MV.EduActivity;
import com.dididoctor.patient.R;
import com.dididoctor.patient.Ui.customlistview.OnRefreshListener;
import com.dididoctor.patient.Ui.customlistview.RefreshListView;
import com.dididoctor.patient.Utils.MyToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： ServiceActivity
 * @包名称： com.dididoctor.patient.Activity.Service
 * @创建人： zhanjunjie
 * @创建时间：2016/4/6 10:18
 * @修改人：
 * @修改时间：2016/4/6 10:18
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class ServiceActivity extends EduActivity implements ServiceView {
    private static final Random random = new Random(System.currentTimeMillis());
    private NotificationManager manager;
    private Notification notification;
    private RefreshListView listView;
    private ServiceAdapter adapter;
    private ServicePresenter presenter;
    private List<ServiceBean> serviceBeans = new ArrayList<>();
    private int pageIndex = 1;

    @Override
    public void init() {
        listView = (RefreshListView) findViewById(R.id.listview);
        adapter = new ServiceAdapter(this, serviceBeans);
        listView.setAdapter(adapter);
        listView.setHeadAndFoot(true, true);
        listView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onPullDownRefresh() {
                pageIndex = 1;
                presenter.getService(pageIndex);
            }

            @Override
            public void onLoadMoring() {
                pageIndex++;
                presenter.getService(pageIndex);
            }

            @Override
            public void onScrollListener(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        presenter = new ServicePresenter(this, this);
        presenter.getService(pageIndex);
    }

    @Override
    public void onClickEvent(View view) {

    }

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_service);
    }

    @Override
    public void getServiceSucced(List<ServiceBean> serviceBeans) {

        if (serviceBeans != null && serviceBeans.size() > 0) {
            if (pageIndex == 1) {
                this.serviceBeans.clear();
                adapter.setData(serviceBeans);
                listView.onRefreshFinish();
            }else{
                if (serviceBeans.size() < ConstantValue.LOAD_MESSAGE_COUNT) {
                    adapter.addData(serviceBeans);
                    listView.showNoMoreData();
                } else {
                    adapter.addData(serviceBeans);
                    listView.hideNoMoreData();
                }
            }
            adapter.notifyDataSetChanged();
        } else {
            if (pageIndex == 1) {
                MyToast.DefaultmakeText(ServiceActivity.this,"暂时还没有合适的套餐推荐于您！",0);
            }else{
                listView.showNoMoreData();
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void getServicefail() {
        listView.onRefreshFinish();
//        listView.hideNoMoreData();
        MyToast.DefaultmakeText(ServiceActivity.this, "您的网络异常请稍候再试！", 0);
    }
}
